package com.atguigu.rest.controller;

import com.atguigu.rest.bean.Employee;
import com.atguigu.rest.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @author yanglin
 * @create 2023-12-20 16:53 星期三
 * description:
 */
@Controller
public class EmployeeController {
    
    @Autowired
    private EmployeeDao employeeDao;
    
    @GetMapping("/employee")
    public String getAllEmployee(Model model){
        Collection<Employee> employeeList = employeeDao.getAll();
        model.addAttribute("employeeList",employeeList);
        return "employee_list";
    }
    
    @DeleteMapping("/employee/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        employeeDao.delete(id);
        //重定向
        return "redirect:/employee";
    }
    
    
    @PostMapping("/employee")
    public String addEmployee(Employee employee){
        employeeDao.save(employee);
        //重定向（重定向默认get请求）
        return "redirect:/employee";
    }
    
    
    @GetMapping("/employee/{id}")
    public String getById(@PathVariable("id") Integer id,Model model){
        Employee employee = employeeDao.get(id);
        model.addAttribute("employee",employee);
        return "employee_update";
    }
    
    @PutMapping("/employee")
    public String update(Employee employee){
        employeeDao.save(employee);
        //重定向到列表
        return "redirect:/employee";
    }
    
}
